package dao.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import dao.ArticleDao;
import domain.Article;

public class ArticleImpl implements ArticleDao {

	public boolean deleteArticle(Article article) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.delete(article);
			s.flush();
			tx.commit();
			return true;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public List findAllArticleByPage(int pageNo, int pageSize) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			int offset=(pageNo-1)*pageSize;
			String hql = "from Article as a order by a.id";
			return s.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize).list();
		} finally {
			if (s != null)
				s.close();
		}
	}

	public Article findArticleById(int id) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			Article article = (Article) s.get(Article.class, id);
			Hibernate.initialize(article.getArticleType());
			Hibernate.initialize(article.getArticleComments());
			return article;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public long findAllCount() {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			String hql = "select count(a.id) from Article as a";
			Object obj= s.createQuery(hql).uniqueResult();
			return (Long)obj;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public boolean saveArticle(Article article) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.save(article);
			tx.commit();
			return true;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public boolean updateArticle(Article article) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.update(article);
			tx.commit();
			return true;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public List findAllArticleByTypeByPage(int articleType_Id, int pageNo,
			int pageSize) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			int offset = (pageNo - 1) * pageSize;
			String hql = "from Article as a where a.articleType.id=:articleType_Id order by a.id";
			return s.createQuery(hql).setLong("articleType_Id", articleType_Id)
					.setFirstResult(offset).setMaxResults(pageSize).list();
		} finally {
			if (s != null)
				s.close();
		}
	}

	public long findCountByType(int articleType_Id) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			String hql = "select count(a.id) from Article as a where a.articleType.id=:articleType_Id";
			Object obj= s.createQuery(hql).setLong("articleType_Id", articleType_Id).uniqueResult();
			return (Long)obj;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public List findTopArticleByType(int articleType_Id, int count) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			String hql = "from Article as a where a.articleType.id=:articleType_Id order by a.readCount desc";
			return s.createQuery(hql).setLong("articleType_Id", articleType_Id)
					.setMaxResults(count).list();
		} finally {
			if (s != null)
				s.close();
		}
	}

	public List findNewArticleByType(int articleType_Id, int count) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			String hql = "from Article as a where a.articleType.id=:articleType_Id order by a.createtime desc";
			return s.createQuery(hql).setLong("articleType_Id", articleType_Id)
					.setMaxResults(count).list();
		} finally {
			if (s != null)
				s.close();
		}
	}

	public List findNewArticle(int count) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			String hql = "from Article as a order by a.createtime desc";
			return s.createQuery(hql).setMaxResults(count).list();
		} finally {
			if (s != null)
				s.close();
		}
	}

	public List findTopArticle(int count) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			String hql = "from Article as a order by a.readCount desc";
			return s.createQuery(hql).setMaxResults(count).list();
		} finally {
			if (s != null)
				s.close();
		}
	}
}
